package com.example.administrator.ctrlled;

import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;

import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_led;
    private FlashUtils utils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_led=findViewById(R.id.imageView);
        utils= new FlashUtils(this);

//

    }

    public void ledOFF(View view) {
        iv_led.setImageResource(R.drawable.led_off);
        utils.close();//关闭手电筒
    }

    public void ledON(View view) {
        iv_led.setImageResource(R.drawable.led_on);
        utils.open();//打开手电筒
    }



    public class FlashUtils {
        private CameraManager manager;
        private Camera mCamera = null;
        private Context context;
        private boolean status = false;//记录手电筒状态

        FlashUtils(Context context){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            }
            this.context = context;
        }

        //打开手电筒
        public void open() {
            if(status){//如果已经是打开状态，不需要打开
                return;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                try {
                    manager.setTorchMode("0", true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                PackageManager packageManager = context.getPackageManager();
                FeatureInfo[] features = packageManager.getSystemAvailableFeatures();
                for (FeatureInfo featureInfo : features) {
                    if (PackageManager.FEATURE_CAMERA_FLASH.equals(featureInfo.name)) { // 判断设备是否支持闪光灯
                        if (null == mCamera) {
                            mCamera = Camera.open();
                        }
                        Camera.Parameters parameters = mCamera.getParameters();
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        mCamera.setParameters(parameters);
                        mCamera.startPreview();
                    }
                }
            }
            status = true;//记录手电筒状态为打开
        }

        //关闭手电筒
        public void close() {
            if(!status){//如果已经是关闭状态，不需要打开
                return;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                try {
                    manager.setTorchMode("0", false);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            } else {
                if (mCamera != null) {
                    mCamera.stopPreview();
                    mCamera.release();
                    mCamera = null;
                }
            }
            status = false;//记录手电筒状态为关闭
        }

    }
}
